<template>
	<view @touchmove.prevent v-if="visibleSync" :class="{ 'uni-drawer--visible': showDrawer }" class="uni-drawer">
		<view class="uni-drawer__mask" :class="{ 'uni-drawer__mask--visible': showDrawer && mask }" @tap="close" />
		<view class="uni-drawer__content" :class="{'uni-drawer--right': rightMode,'uni-drawer--left': !rightMode, 'uni-drawer__content--visible': showDrawer}">
			<view class="drawer_content">
				<!-- 头部 -->
				<view class="tips_title">更多筛选条件</view>
				<!-- 内容列表 -->
				<view class="content_ul">
					<view class="list_li" v-if="userInfo.role == '148'">
						<view class="screenTit">意向车型</view>
						<view class="screenVal" @click="sheetTap(0)">
							{{clueDrawerForm.intentCarStyles ? clueDrawerForm.intentCarStyles : '请选择'}}
							<uni-icons class="rightIcon" color="#3B86FF" size="18" type="forward" />
						</view>
					</view>
					<view class="list_li">
						<view class="screenTit">来源渠道</view>
						<view class="screenVal">
							<channel-picker :defaultRegion="[0, 0]"  @getRegion="handleGetRegion">
								{{clueDrawerForm.channelName ? clueDrawerForm.channelName : '请选择'}}
								<uni-icons class="rightIcon" color="#3B86FF" size="18" type="forward" />
							</channel-picker>
						</view>
					</view>
					<view class="list_li">
						<view class="screenTit">清洗状态</view>
						<view class="screenVal" @click="sheetTap(1)">
							{{ clueDrawerForm.cleanStatus && clueDrawerForm.cleanStatus =='todo' ? '未清洗' : (clueDrawerForm.cleanStatus && clueDrawerForm.cleanStatus =='done' ? '已人工清洗' : '请选择') }}
							<uni-icons class="rightIcon" color="#3B86FF" size="18" type="forward" />
						</view>
					</view>
					<view class="list_li">
						<view class="screenTit">创建时间</view>
						<view class="screenVal" @click="dateModeAction(0)">
							{{clueDrawerForm.createStartTime && clueDrawerForm.createEndTime ? clueDrawerForm.createStartTime+ '—' +clueDrawerForm.createEndTime : '请选择'}}
							<uni-icons class="rightIcon" color="#3B86FF" size="18" type="forward" />
						</view>
					</view>
					<view class="list_li" v-if="userInfo.role == '148'">
						<view class="screenTit">派发时间</view>
						<view class="screenVal" @click="dateModeAction(1)">
							{{clueDrawerForm.distributeStartTime && clueDrawerForm.distributeEndTime ? clueDrawerForm.distributeStartTime+ '—' +clueDrawerForm.distributeEndTime : '请选择'}}
							<uni-icons class="rightIcon" color="#3B86FF" size="18" type="forward" />
						</view>
					</view>
					<view class="list_li">
						<view class="screenTit">分配时间</view>
						<view class="screenVal" @click="dateModeAction(2)">
							{{clueDrawerForm.assignStartTime && clueDrawerForm.assignEndTime ? clueDrawerForm.assignStartTime+ '—' +clueDrawerForm.assignEndTime : '请选择'}}
							<uni-icons class="rightIcon" color="#3B86FF" size="18" type="forward" />
						</view>
					</view>
				</view>
				<view class="footer_btn">
					<view class="btn" @click="resetFun()">重置</view>
					<view class="btn noteBtn" @click="subFun()">确认</view>
				</view>
			</view>
		</view>
		
		<date-range v-if="showMutibleDate" :mode="viewMode" :showMutibleDate="showMutibleDate" :startDate="startDate"
		 :endDate="endDate" @onSelected="dateSelected($event, typeDatePop)"></date-range>
	</view>
</template>

<script>
	/**
	 * Drawer 抽屉
	 * @description 抽屉侧滑菜单
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=26
	 * @property {Boolean} visible = [true|false] Drawer的显示状态
	 * @property {Boolean} mask = [true | false] 是否显示遮罩
	 * @property {Boolean} mode = [left | right] Drawer 滑出位置
	 * 	@value left 从左侧滑出
	 * 	@value right 从右侧侧滑出
	 * @event {Function} close 组件关闭时触发事件
	 */
	import uniIcons from "@/components/uni-icons/uni-icons.vue";
	import dateRange from '@/components/zxp-datepickerRange/zxp-datepickerRange.vue';
	import channelPicker from './channelPicker.vue'
	export default {
		name: 'UniDrawer',
		props: {
			/**
			 * 显示状态
			 */
			visible: {
				type: Boolean,
				default: false
			},
			/**
			 * 显示模式（左、右），只在初始化生效
			 */
			mode: {
				type: String,
				default: 'right'
			},
			/**
			 * 蒙层显示状态
			 */
			mask: {
				type: Boolean,
				default: true
			},
			clueDrawerForm: {
				type: Object,
				default: {}
			}
		},
		components: {
			uniIcons,
			dateRange,
			channelPicker
		},
		data() {
			return {
				visibleSync: false,
				showDrawer: false,
				rightMode: false,
				watchTimer: null,
				userInfo: {},
				modelList: ['车系&车型', '车型'],
				channelList: ['一级渠道&二级渠道', '一级渠道', '二级渠道'],
				cleanList: ['已人工清洗', '未清洗'],
				dateValue: [],
				startDate: '2020-01-01',
				endDate: '',
				showMutibleDate: false,
				viewMode: 'date',
				typeDatePop: '',
			}
		},
		watch: {
			visible(val) {
				if (val) {
					this.open()
				} else {
					this.close()
				}
			},
			clueDrawerForm(newVal) {
				this.clueDrawerForm = newVal
			}
		},
		created() {
			this.visibleSync = this.visible
			setTimeout(() => {
				this.showDrawer = this.visible
			}, 100)
			this.rightMode = this.mode === 'right'

			this.userInfo = uni.getStorageSync('user_info')
		},
		methods: {
			close() {
				this._change('showDrawer', 'visibleSync', false)
			},
			open() {
				this._change('visibleSync', 'showDrawer', true)
			},
			_change(param1, param2, status) {
				console.log(param1, param2, status)
				this[param1] = status
				if (this.watchTimer) {
					clearTimeout(this.watchTimer)
				}
				this.watchTimer = setTimeout(() => {
					this[param2] = status
					this.$emit(status ? 'open' : 'close')
				}, status ? 50 : 300)
			},

			sheetTap(flag) {
				if(flag == '0') {
					uni.navigateTo({
						url: '/pages/clue/series/index'
					})
				} else if(flag == '1') {
					uni.showActionSheet({
						itemList: this.cleanList,
						success: (e) => {
							// 已人工清洗、未清洗
							if (this.cleanList[e.tapIndex] == '未清洗') {
								this.clueDrawerForm.cleanStatus = 'todo'
							} else {
								this.clueDrawerForm.cleanStatus = 'done'
							}
					
						}
					})
				}
				
			},

			dateModeAction(flag) {
				if (flag == '0') { //创建时间
					this.startDate = this.clueDrawerForm.createStartTime || this.startDate
					this.endDate = this.clueDrawerForm.createEndTime
				} else if (flag == '1') { //派发时间
					this.startDate = this.clueDrawerForm.distributeStartTime || this.startDate
					this.endDate = this.clueDrawerForm.distributeEndTime
				} else if (flag == '2') { //分配时间
					this.startDate = this.clueDrawerForm.assignStartTime || this.startDate
					this.endDate = this.clueDrawerForm.assignEndTime
				}
				this.typeDatePop = flag
				this.viewMode = 'date'
				this.showMutibleDate = true
			},

			dateSelected(e, flag) {
				if (e) {
					if (flag == '0') {
						this.clueDrawerForm.createStartTime = e.start
						this.clueDrawerForm.createEndTime = e.end
					} else if (flag == '1') {
						this.clueDrawerForm.distributeStartTime = e.start
						this.clueDrawerForm.distributeEndTime = e.end
					} else if (flag == '2') {
						this.clueDrawerForm.assignStartTime = e.start
						this.clueDrawerForm.assignEndTime = e.end
					}
				}
				this.showMutibleDate = false
			},

			handleGetRegion(region) {
				this.clueDrawerForm.firstChannelIds = region[0].channelId
				this.clueDrawerForm.secondChannelIds = region[1].channelId
				
				this.clueDrawerForm.channelName = region[1].channelId ? region[1].name : region[0].name
			},

			subFun() {
				this.$emit('screenReturn', this.clueDrawerForm, false)
			},

			resetFun() {
				this.$store.commit('changeIntentionCar', {})
				this.$emit('screenReturn', {
					cleanStatus: '',
					salesmanIds: '',
					intentCarModels: '',
					intentCarStyles: '',
					firstChannelIds: '',
					secondChannelIds: '',
					channelName: '',
					createStartTime: '',
					createEndTime: '',
					distributeStartTime: '',
					distributeEndTime: '',
					assignStartTime: '',
					assignEndTime: ''
				}, true)
			}

		}
	}
</script>

<style lang="scss" scoped>
	/* 抽屉宽度 */
	.uni-drawer {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		overflow: hidden;
		z-index: 999;
	}

	.uni-drawer__content {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		position: absolute;
		top: 0;
		// width: 526rpx;
		width: 75%;
		bottom: 0;
		background-color: #ffffff;
		transition: transform 0.3s ease;
	}

	.uni-drawer--left {
		left: 0;
		transform: translateX(-516rpx);
	}

	.uni-drawer--right {
		right: 0;
		transform: translateX(516rpx);
	}

	.uni-drawer__content--visible {
		transform: translateX(0px);
	}


	.uni-drawer__mask {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		opacity: 0;
		position: absolute;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		background-color: rgba(0, 0, 0, 0.4);
		transition: opacity 0.3s;
	}

	.uni-drawer__mask--visible {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		opacity: 1;
	}

	.drawer_content {
		width: 100%;
		padding: 74rpx 0 0 56rpx;
		box-sizing: border-box;
		position: relative;

		.tips_title {
			font-size: 28rpx;
			font-weight: 500;
			line-height: 48rpx;
			color: #43425D;
		}

		.content_ul {
			width: 100%;
			box-sizing: border-box;
			margin-top: 30rpx;
			position: relative;

			.list_li {
				font-size: 24rpx;
				color: #43425D;
				padding: 30rpx 30rpx 30rpx 0;
				border-bottom: 1rpx solid #F2F6F8;
				width: 100%;
				display: flex;
				display: -webkit-flex;
				display: inline-flex;
				align-items: center;
				justify-content: space-between;
				box-sizing: border-box;
				line-height: 48rpx;

				.screenVal {
					flex: 1;
					font-size: 20rpx;
					color: #3B86FF;
					text-align: right;

					.rightIcon {
						position: relative;
						top: 8rpx;
					}
				}

				// &:last-child {
				// 	border-bottom: none;
				// }
			}
		}

		.footer_btn {
			width: 100%;
			position: fixed;
			bottom: 0;
			left: 0;
			display: flex;
			display: -webkit-flex;
			display: inline-flex;

			.btn {
				flex: 1;
				height: 64rpx;
				line-height: 64rpx;
				text-align: center;
				font-size: 20rpx;
				color: #3B86FF;
				border: 2rpx solid #3B86FF;
				border-left: none;
				box-sizing: border-box;
			}

			.noteBtn {
				border: none;
				background: #3B86FF;
				color: #FFFFFF;
			}
		}
	
	
	}
</style>
